<Project TreatAsLocalProperty="ArchiveTests">
  <Import Project="tests.wasm.targets" />

  <!-- We need to set this in order to get extensibility on xunit category traits and other arguments we pass down to xunit via MSBuild properties -->
  <PropertyGroup>
    <IsWasiProject Condition="'$(IsWasiProject)' == ''">true</IsWasiProject>

    <WasmSingleFileBundle Condition="'$(WasmSingleFileBundle)' == ''">false</WasmSingleFileBundle>
    <WasmMainAssemblyFileName Condition="'$(WasmMainAssemblyFileName)' == ''">WasmTestRunner.dll</WasmMainAssemblyFileName>

    <InstallWasmtimeForTests Condition="'$(InstallWasmtimeForTests)' == '' and
                                        ('$(ContinuousIntegrationBuild)' != 'true' or Exists('/.dockerenv'))"
                                        >true</InstallWasmtimeForTests>

    <!--<InstallWorkloadUsingArtifactsDependsOn>_GetWorkloadsToInstall;$(InstallWorkloadUsingArtifactsDependsOn)</InstallWorkloadUsingArtifactsDependsOn>-->
    <WASI_SDK_PATH Condition="'$(WASI_SDK_PATH)' == ''">$([MSBuild]::NormalizeDirectory($(MonoProjectRoot), 'wasi', 'wasi-sdk'))</WASI_SDK_PATH>

    <_BundleAOTTestWasmAppForHelixDependsOn>$(_BundleAOTTestWasmAppForHelixDependsOn);PrepareForWasiBuildApp;_PrepareForAOTOnHelix</_BundleAOTTestWasmAppForHelixDependsOn>
  </PropertyGroup>

  <!-- On CI this is installed as part of pretest, but it should still be installed
       for WBT, and debugger tests -->
  <Import Project="$(MSBuildThisFileDirectory)wasi-provisioning.targets" />

  <PropertyGroup>
    <_AppArgs Condition="'$(WasmSingleFileBundle)' == 'true'">$([System.IO.Path]::GetFileNameWithoutExtension('$(WasmMainAssemblyFileName)')).wasm</_AppArgs>
    <_AppArgs Condition="'$(WasmSingleFileBundle)' != 'true'">dotnet.wasm WasmTestRunner</_AppArgs>

    <_AppArgs Condition="'$(IsFunctionalTest)' != 'true' and '$(WasmSingleFileBundle)' != 'true'">$(_AppArgs) managed/$(AssemblyName).dll</_AppArgs>
    <_AppArgs Condition="'$(IsFunctionalTest)' != 'true' and '$(WasmSingleFileBundle)' == 'true'">$(_AppArgs) $(AssemblyName).dll</_AppArgs>
    <_AppArgs Condition="'$(WasmTestAppArgs)' != ''">$(_AppArgs) -- $(WasmTestAppArgs)</_AppArgs>

    <!-- FIXME: wasttime specific param name -->
    <WasmXHarnessMonoArgs Condition="'$(XunitShowProgress)' == 'true'">$(WasmXHarnessMonoArgs) --env=XHARNESS_LOG_TEST_START=true</WasmXHarnessMonoArgs>
  </PropertyGroup>

  <PropertyGroup Condition="'$(RunScriptCommand)' == ''">
    <_XHarnessArgs Condition="'$(OS)' != 'Windows_NT'">wasi $XHARNESS_COMMAND --app=. --output-directory=$XHARNESS_OUT</_XHarnessArgs>
    <_XHarnessArgs Condition="'$(OS)' == 'Windows_NT'">wasi %XHARNESS_COMMAND% --app=. --output-directory=%XHARNESS_OUT%</_XHarnessArgs>

    <!-- FIXME: workaround till xharness correctly defaults to using wasmtime.exe -->
    <_XHarnessArgs Condition="'$(OS)' == 'Windows_NT' and '$(ContinuousIntegrationBuild)' != 'true'">$(_XHarnessArgs) --wasm-engine-path=$(WasmtimeDir)wasmtime.exe</_XHarnessArgs>
    <_XHarnessArgs Condition="'$(OS)' == 'Windows_NT' and '$(ContinuousIntegrationBuild)' == 'true'">$(_XHarnessArgs) --wasm-engine-path=%HELIX_CORRELATION_PAYLOAD%\wasmtime\wasmtime.exe</_XHarnessArgs>

    <_XHarnessArgs Condition="'$(WasmSingleFileBundle)' != 'true'" >$(_XHarnessArgs) --engine-arg=--dir=.</_XHarnessArgs>
    <_XHarnessArgs Condition="'$(IsFunctionalTest)' == 'true'"     >$(_XHarnessArgs) --expected-exit-code=$(ExpectedExitCode)</_XHarnessArgs>
    <_XHarnessArgs Condition="'$(WasmXHarnessArgs)' != ''"         >$(_XHarnessArgs) $(WasmXHarnessArgs)</_XHarnessArgs>
    <_XHarnessArgs Condition="'$(WasmXHarnessTestsTimeout)' != ''" >$(_XHarnessArgs) &quot;--timeout=$(WasmXHarnessTestsTimeout)&quot;</_XHarnessArgs>
    <_XHarnessArgs                                                 >$(_XHarnessArgs) --engine-arg=--max-wasm-stack=134217728</_XHarnessArgs>
    <_XHarnessArgs Condition="'$(WasmXHarnessArgsCli)' != ''"      >$(_XHarnessArgs) $(WasmXHarnessArgsCli)</_XHarnessArgs>

    <_InvariantGlobalization Condition="'$(InvariantGlobalization)' == 'true'">--env=DOTNET_SYSTEM_GLOBALIZATION_INVARIANT=true</_InvariantGlobalization>

    <!-- There are two flavors of WasmXHarnessArgs and WasmXHarnessMonoArgs, one is MSBuild property and the other is environment variable -->
    <RunScriptCommand Condition="'$(OS)' != 'Windows_NT'">$HARNESS_RUNNER $(_XHarnessArgs) %24XHARNESS_ARGS %24WasmXHarnessArgs -- $(WasmXHarnessMonoArgs) %24WasmXHarnessMonoArgs $(_InvariantGlobalization) %24_InvariantGlobalization  $(_AppArgs) %24WasmTestAppArgs</RunScriptCommand>
    <RunScriptCommand Condition="'$(OS)' == 'Windows_NT'">%HARNESS_RUNNER% $(_XHarnessArgs) %XHARNESS_ARGS% %WasmXHarnessArgs%  -- $(WasmXHarnessMonoArgs) %WasmXHarnessMonoArgs% $(_InvariantGlobalization) %_InvariantGlobalization% $(_AppArgs) %WasmTestAppArgs%</RunScriptCommand>
  </PropertyGroup>

  <PropertyGroup Condition="'$(BuildAOTTestsOnHelix)' == 'true'">
    <_AOTBuildCommand Condition="'$(BrowserHost)' != 'windows'">_buildAOTFunc publish/ProxyProjectForAOTOnHelix.proj $XHARNESS_OUT/AOTBuild.binlog</_AOTBuildCommand>
    <_AOTBuildCommand Condition="'$(BrowserHost)' == 'windows'">dotnet msbuild publish/ProxyProjectForAOTOnHelix.proj /bl:%XHARNESS_OUT%/AOTBuild.binlog</_AOTBuildCommand>

    <!-- running aot-helix tests locally, so we can test with the same project file as CI -->
    <_AOTBuildCommand Condition="'$(ContinuousIntegrationBuild)' != 'true'">$(_AOTBuildCommand) /p:RuntimeSrcDir=$(RepoRoot) /p:RuntimeConfig=$(Configuration) /p:TasksConfiguration=$(TasksConfiguration)</_AOTBuildCommand>

    <_AOTBuildCommand>$(_AOTBuildCommand) /p:RunAOTCompilation=$(RunAOTCompilation)</_AOTBuildCommand>
    <_AOTBuildCommand>$(_AOTBuildCommand) $(_ShellCommandSeparator) cd wasm_build/AppBundle</_AOTBuildCommand>

    <RunScriptCommand Condition="'$(RunScriptCommand)' == ''">$(_AOTBuildCommand)</RunScriptCommand>
    <RunScriptCommand Condition="'$(RunScriptCommand)' != ''">$(_AOTBuildCommand) $(_ShellCommandSeparator) $(RunScriptCommand)</RunScriptCommand>
  </PropertyGroup>

  <!-- Don't include InTree.props here, because the test projects themselves can set the target* properties -->
  <Import Project="$(WasiProjectRoot)build\WasiApp.props"
          Condition="'$(BuildAOTTestsOn)' == 'local'" />
  <Import Project="$(WasiProjectRoot)build\WasiApp.InTree.targets"
          Condition="'$(BuildAOTTestsOn)' == 'local'" />

  <PropertyGroup Condition="'$(BuildAOTTestsOnHelix)' == 'true'">
    <!-- wasm targets are not imported at all, in this case, because we run the wasm build on helix -->
  </PropertyGroup>

  <PropertyGroup Condition="'$(BuildAOTTestsOnHelix)' != 'true'">
    <WasmBuildOnlyAfterPublish>true</WasmBuildOnlyAfterPublish>

    <!-- wasm's publish targets will trigger publish, so we shouldn't do that -->
    <PublishTestAsSelfContainedDependsOn />
    <WasmNestedPublishAppDependsOn>PrepareForWasiBuildApp;$(WasmNestedPublishAppDependsOn)</WasmNestedPublishAppDependsOn>
  </PropertyGroup>

  <Target Name="PrepareForWasiBuildApp">
    <PropertyGroup>
      <WasmAppDir>$(BundleDir)</WasmAppDir>
      <WasmInvariantGlobalization>$(InvariantGlobalization)</WasmInvariantGlobalization>

      <WasmNativeDebugSymbols Condition="'$(DebuggerSupport)' == 'true' and '$(WasmNativeDebugSymbols)' == ''">true</WasmNativeDebugSymbols>
      <!--
        Do this *after* importing BrowserWasmApp.targets. tests.wasm.targets sets this to `reset-to-zero` to indicate
        that we want to force this value to zero.

        BrowserWasmApp.targets *overrides* `WasmDebugLevel` when `DebuggerSupport=true`, but for the library tests
        we explicitly want to:
        1. build with DebuggerSupport=true so the debugger attributes are preserved by the linker;
        2. *debugging* is disabled at run time so the interpreter optimizations don't get disabled.
      -->
      <WasmDebugLevel Condition="'$(WasmDebugLevel)' == 'reset-to-zero'">0</WasmDebugLevel>
    </PropertyGroup>

    <ItemGroup Condition="'$(IncludeSatelliteAssembliesInVFS)' == 'true' and '$(BuildAOTTestsOnHelix)' != 'true'">
      <_SatelliteAssemblies Include="$(PublishDir)*\*.resources.dll" />
      <_SatelliteAssemblies CultureName="$([System.IO.Directory]::GetParent('%(Identity)').Name)" />
      <_SatelliteAssemblies TargetPath="%(CultureName)\%(FileName)%(Extension)" />

      <WasmFilesToIncludeInFileSystem Include="@(_SatelliteAssemblies)" />
    </ItemGroup>

    <ItemGroup>
      <WasmAssembliesToBundle Include="$(PublishDir)\**\*.dll" Condition="'$(BuildAOTTestsOnHelix)' == 'true'" />
      <WasmFilesToIncludeInFileSystem Include="@(ContentWithTargetPath)" />

      <_CopyLocalPaths
              Include="@(PublishItemsOutputGroupOutputs)"
              Condition="'%(PublishItemsOutputGroupOutputs.BuildReference)' == 'true' and
                         !$([System.String]::new('%(PublishItemsOutputGroupOutputs.Identity)').EndsWith('.resources.dll'))" />

      <_CopyLocalPaths TargetPath="%(_CopyLocalPaths.RelativePath)" Condition="'%(_CopyLocalPaths.RelativePath)' != ''" />
      <_CopyLocalPaths TargetPath="%(FileName)%(Extension)"         Condition="'%(_CopyLocalPaths.RelativePath)' == ''" />
      <WasmFilesToIncludeInFileSystem Include="@(_CopyLocalPaths)" />

      <!-- Include files specified by test projects from publish dir -->
      <WasmFilesToIncludeInFileSystem
              Include="$(PublishDir)%(WasmFilesToIncludeFromPublishDir.Identity)"
              TargetPath="%(WasmFilesToIncludeFromPublishDir.Identity)"
              Condition="'%(WasmFilesToIncludeFromPublishDir.Identity)' != ''" />
    </ItemGroup>
  </Target>

  <Target Name="_PrepareForAOTOnHelix">
    <ItemGroup>
      <_WasmPropertyNames Include="DisableParallelEmccCompile" />
      <_WasmPropertyNames Include="WasiClangCompileOptimizationFlag" />
      <_WasmPropertyNames Include="WasiClangLinkOptimizationFlag" />
      <_WasmPropertyNames Include="WasmIncludeFullIcuData" />
      <_WasmPropertyNames Include="WasmIcuDataFileName" />
      <_WasmPropertyNames Include="HybridGlobalization" />
    </ItemGroup>
  </Target>

  <Target Name="_WasiAddToRunScript" BeforeTargets="GenerateRunScript">
    <ItemGroup Condition="'$(OS)' != 'Windows_NT'">
      <SetScriptCommands Condition="'$(InstallWasmtimeForTests)' == 'true' and Exists($(WasmtimeDir))" Include="export PREPEND_PATH=$(WasmtimeDir)" />
      <SetScriptCommands Condition="'$(IsWasiProject)' == 'true'" Include="export RuntimeIdentifier=wasi-wasm" />
    </ItemGroup>
    <ItemGroup Condition="'$(OS)' == 'Windows_NT'">
      <SetScriptCommands Condition="'$(InstallWasmtimeForTests)' == 'true' and Exists($(WasmtimeDir))" Include="set PREPEND_PATH=$(WasmtimeDir)" />
      <SetScriptCommands Condition="'$(IsWasiProject)' == 'true'" Include="set RuntimeIdentifier=wasi-wasm" />
    </ItemGroup>
  </Target>
</Project>
